﻿using System.Collections.Generic;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using Avalonia.Threading;
using MilkTeaMachine.OfflineClient.EntityFrameworkCore.Models;
using MilkTeaMachine.OfflineClient.ViewModels;
using MsBox.Avalonia;
using ReactiveUI;

namespace MilkTeaMachine.OfflineClient.Views;

public partial class
    IngredientCalibrationSelectorUserControl : ReactiveUserControl<IngredientCalibrationSelectorViewModel>
{
    public IngredientCalibrationSelectorViewModel ViewModelLocal => ViewModel!;

    public IngredientCalibrationSelectorUserControl()
    {
        AvaloniaXamlLoader.Load(this);
        this.WhenActivated(_ => { });
    }

    private static readonly Dictionary<int, string> AddressNameDic = new()
    {
        { 21, "B101" },
        { 22, "B102" },
        { 23, "B103" },
        { 24, "B104" },
        { 31, "B105" },
        { 32, "B106" },
        { 33, "B107" },
        { 34, "B108" },
        { 41, "B109" },
        { 42, "B110" },
        { 43, "B111" },
        { 44, "B112" },
        { 51, "B113" },
        { 52, "B114" },
        { 53, "B115" },
        { 54, "B116" },
        { 11, "B125" },
        { 12, "B126" },
        { 13, "B127" },
        { 14, "B128" },
        { 15, "B129" },
        { 16, "B130" },
    };

    private void SelectingItemsControl_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        if (e.RemovedItems.Count != 0 &&
            e.RemovedItems[0] is Ingredient ingredientRemove)
        {
            if (e.AddedItems.Count != 0 &&
                e.AddedItems[0] is Ingredient ingredientAdd)
            {
                //检查是否已被选中
                var selectedList = ViewModelLocal.CheckSelectedList(ingredientAdd);
                if (ingredientRemove.Id != -1 && ingredientAdd.Id == -1 && selectedList.Count > 1)
                {
                    ViewModelLocal.DeleteHashSet.Add(ingredientRemove.Id);
                    return;
                }

                var comboBox = (ComboBox)sender!;
                
                if ((selectedList.Count > 0 && selectedList.IndexOf(ingredientAdd) == -1) || selectedList.Count > 1)
                {
                    MessageBoxManager
                        .GetMessageBoxStandard("提示",
                            $"原料[{ingredientAdd.IngredientName}]已被绑定到[{AddressNameDic[selectedList[0].DeviceAddress]}]地址上")
                        .ShowWindowAsync();
                    Dispatcher.UIThread.InvokeAsync(() =>
                    {
                        comboBox.SelectedValue = ViewModelLocal.Default;
                        ViewModelLocal.DeleteHashSet.Add(ingredientRemove.Id);
                    });
                    if (ingredientRemove.Id != -1)
                    {
                        //删除
                        ViewModelLocal.DeleteHashSet.Add(ingredientRemove.Id);
                        var existInAdd = ViewModelLocal.AddHashSet.FirstOrDefault(add => add.Id == ingredientRemove.Id);
                        if (existInAdd is not null &&
                            e.AddedItems.Count != 0 &&
                            ((Ingredient)e.AddedItems[0]!).Id != -1)
                        {
                            ViewModelLocal.AddHashSet.Remove(existInAdd);
                        }
                    }
                }
                else
                {
                    Dispatcher.UIThread.InvokeAsync(() =>
                        (comboBox.SelectedValue as Ingredient)!.DeviceAddress = comboBox.TabIndex);
                    if (ingredientAdd.Id != -1)
                    {
                        var selectedValue = (comboBox.SelectedValue as Ingredient)!;
                        ViewModelLocal.AddHashSet.Add(selectedValue);
                        if (ingredientRemove.Id != -1)
                        {
                            ViewModelLocal.DeleteHashSet.Add(ingredientRemove.Id);
                        }
                    }
                }
            }
        }
    }
}